package com.example.demo.mp.controller;

import cn.hutool.core.util.StrUtil;
import com.example.demo.mp.dto.AnnouncementCategoryQuery;
import com.example.demo.mp.entity.Announcement;
import com.example.demo.mp.entity.AnnouncementCategory;
import com.example.demo.mp.service.AnnouncementCategoryService;
import com.example.demo.mp.service.AnnouncementService;
import com.example.demo.mp.vo.AnnouncementCategoryItemVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("AnnouncementCategory")
public class AnnouncementCategoryController {

    @Resource
    AnnouncementCategoryService categoryService;

    @Resource
    AnnouncementService announcementService;

    @GetMapping("listAll")
    public List<AnnouncementCategory> listAll() {
        return categoryService.list();
    }

    List<AnnouncementCategory> cacheList;

    @GetMapping("query")
//    public List<AnnouncementCategory> list(AnnouncementCategoryQuery query) {
    public List<AnnouncementCategory> list(@RequestParam(value = "name",required=false) String name,
                                           @RequestParam(required=false) Date startTime,
                                           @RequestParam(required=false) Date endTime) {
       if(cacheList!=null){
           return cacheList;
       }
        cacheList= categoryService.query()
                .like(StrUtil.isNotEmpty(name),"name",name)
                .between(startTime!=null&&endTime!=null,"created_at",startTime,endTime)
                .list();
        return cacheList;
    }

    //不允许
    @GetMapping("queryList")
    public List<AnnouncementCategoryItemVO> listAll(@RequestParam(value = "ids",required=false) List<Long> ids ) {
        List<AnnouncementCategory> categories= categoryService.lambdaQuery()
                .in(AnnouncementCategory::getId,ids).list();
        List<AnnouncementCategoryItemVO> categoryItemVOs=new ArrayList<>();
        for (AnnouncementCategory category : categories) {

            AnnouncementCategoryItemVO categoryItemVO=new AnnouncementCategoryItemVO();
            categoryItemVO.setCategory(category);
            categoryItemVO.setAnnouncements(announcementService.lambdaQuery()
                    .eq(Announcement::getCategoryId,category.getId())
                    .list());
            categoryItemVOs.add(categoryItemVO);

        }
        return categoryItemVOs;
    }

    @GetMapping("queryList2")
    public List<AnnouncementCategoryItemVO> listAll2(@RequestParam(value = "ids",required=false) List<Long> ids ) {
        List<AnnouncementCategory> categories= categoryService.lambdaQuery()
                .in(AnnouncementCategory::getId,ids).list();
        List<Announcement>  announcements =announcementService.lambdaQuery()
                .in(Announcement::getCategoryId,ids)
                .list();

        List<AnnouncementCategoryItemVO> categoryItemVOs=new ArrayList<>();
        for (AnnouncementCategory category : categories) {
            for (Announcement announcement : announcements) {
                if(category.getId().equals(announcement.getCategoryId())){
                    AnnouncementCategoryItemVO categoryItemVO=new AnnouncementCategoryItemVO();
                    categoryItemVO.setCategory(category);
                    categoryItemVO.setAnnouncements(announcements);
                    categoryItemVOs.add(categoryItemVO);
                }
            }
        }

        return categoryItemVOs;
    }

    @PostMapping(value = "add")
    public AnnouncementCategory add(@RequestBody  AnnouncementCategory category){
         categoryService.save(category);
        return category;
    }

    @PostMapping(value = "addList")
    public boolean addList(@RequestBody List<AnnouncementCategory> ls){
        categoryService.saveBatch(ls);
        //
//        for (AnnouncementCategory category:ls){
//            categoryService.save(category);
//        }
        return true;
    }



    @PostMapping(value = "updateById")
    public AnnouncementCategory updateById(@RequestBody   AnnouncementCategory category){
        categoryService.updateById(category);
        return category;
    }
    @PostMapping(value = "update")
    public AnnouncementCategory update(@RequestBody AnnouncementCategory category){
        categoryService.lambdaUpdate()
                .eq(AnnouncementCategory::getId,category.getId())
                .set(AnnouncementCategory::getDescription,category.getDescription());
        return category;
    }

}
